<?php
/**
 * Created by PhpStorm.
 * User: chenxiaogang
 * Date: 2017/11/28
 * Time: 16:04
 */

namespace User\Model;

use Common\Model\CommonModel;
class OrderPay extends CommonModel
{
    protected $connection = 'DB_CONFIG2';
    protected $trueTableName = 'order_pay';

    /**
     * @use          [交易记录]
     * @author       chenxiaogang
     * @param $where
     * @param $limit
     * @return mixed
     */
    public function get_report_list($where,$limit)
    {
        $data = $this->where($where)->limit($limit)->select();
        return $data;
    }

    /**
     * @use          [交易记录条数]
     * @author       chenxiaogang
     * @param $where
     * @return mixed
     */
    public function get_report_list_count($where)
    {
        $data = $this->where($where)->count('1');
        return $data;
    }

    /**
     * @use          [得到交易记录统计 - 按照自然日、自然周、自然月统计]
     * @author       chenxiaogang
     * @return array
     */
    public function get_report_group()
    {
        $where = $this->get_report_group_conditon();
        $request=I('request.');
        $request['timer_type']=empty($request['timer_type'])?0:$request['timer_type'];
        $select = [
            'examine_id','sum(amount) as total_amount'
        ];
        switch ($request['timer_type']){
            case '1'://日
                $select[]="DATE_FORMAT(crtime,'%Y-%m-%d') as thedate_time";
                break;
            case '2'://周
                $select[]="CONCAT(DATE_SUB(DATE_FORMAT(crtime,'%Y-%m-%d'),INTERVAL WEEKDAY(DATE_FORMAT(crtime,'%Y-%m-%d'))+1 DAY),' ',DATE_SUB(DATE_FORMAT(crtime,'%Y-%m-%d'),INTERVAL WEEKDAY(DATE_FORMAT(crtime,'%Y-%m-%d'))-5 DAY)) as thedate_time";
                break;
            case '3'://月
                $select[]="CONCAT(DATE_FORMAT(LAST_DAY(crtime),'%Y-%m-'),'01',' ',LAST_DAY(crtime)) as thedate_time";
                break;
            default://日
                $select[]="DATE_FORMAT(crtime,'%Y-%m-%d') as thedate_time";
                break;
        }
        $data = $this->field($select)->where($where)->group('thedate_time,examine_id')->select();
        return $data;
    }

    /**
     * @use          [得到交易记录统计的条件]
     * @author       chenxiaogang
     */
    private function get_report_group_conditon(){
        $where = [];
        if($admin = I('request.admin')){
            $examineIds = M('Users')->where(['user_login'=>['like','%'.$admin.'%']])->getField('id');
            !empty($examineIds) && ($where['examine_id'] = ['in',$examineIds]);
        }
        //如果两个日期都没有传，默认当天
        $starts = I('request.starts');
        $ends = I('request.ends');
        if(!$starts && !$ends){
            $current_date = date('Y-m-d');
            $where['crtime'] = ['EGT',$current_date.' 00:00:00'];
            $where['crtime '] = ['ELT',$current_date.' 23:59:59'];
        }else{
            $starts && ($where['crtime'] = ['EGT',$starts.' 00:00:00']);
            $ends && ($where['crtime '] = ['ELT',$ends.' 23:59:59']);
        }
        $where['status'] = 1;//必要条件
        return $where;
    }

}